  cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
use "\\micro.intra\Projekt\P1129$\P1129_Gem\Politikerdata\Data files\pol_ko.dta", clear
 joinby p_id valar using "\\micro.intra\Projekt\P1129$\P1129_Gem\Politikerdata\Data files\pol_ri.dta", unmatched(both)
  drop _merge
   joinby p_id valar using "\\micro.intra\Projekt\P1129$\P1129_Gem\Politikerdata\Data files\pol_lt.dta", unmatched(both)
  drop _merge
   joinby p_id valar using "\\micro.intra\Projekt\P1129$\P1129_Gem\Politikerdata\Data files\uppdrag.dta", unmatched(both)
  drop _merge
  gen year=valar
    joinby p_id year using  pol_incpanel, unmatched(master)

drop _merge
joinby p_id year using edu_panel_pol, unmatched(master)
replace parti_initial=parti_initial_ri if parti_initial==""
replace parti_initial=parti_initial_lt if parti_initial==""
drop _merge year
  gen year= 1970 if valar==1970
  replace year= 1970 if valar==1973
    replace year= 1975 if valar==1976
	  replace year= 1975 if valar==1979
	    replace year= 1980 if valar==1982
		  replace year= 1985 if valar==1985
		    replace year= 1985 if valar==1988
				replace year= 1990 if valar==1991
					replace year= 1990 if valar==1994
					replace year= valar if valar>=1998
					
			 
  joinby p_id year using sei_panel_pol, unmatched (both)
  drop _merge

    joinby p_id year using party_org_year, unmatched (master)
  drop _merge
  
      joinby p_id year using edu_inc_seiyears, unmatched (master)
  drop _merge

  replace mun=llkk if llkk!=""
  replace llkk = mun if llkk==""
  do "mun code changes"
  drop age woman
  joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta" , unmatched(master)
  destring FodelseAr Kon, force replace
  gen age =year-Fodelse
 gen woman = Kon==2
  drop FodelseAr Kon _merge
  gen non_nordic=  VarldsdelNamn!="Sverige" &VarldsdelNamn!="Norden utom Sverige"
gen non_oecd= non_nordic==1 &  VarldsdelNamn!="EU28 utom Norden" & VarldsdelNamn!="Europa utom EU28 och Norden" &VarldsdelNamn!="Nordamerika"

 drop mun
 gen mun=llkk
joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\draft_data.dta" , unmatched(master)
 drop _merge

 joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\inc_res.dta" , unmatched(master)
  drop _merge
 joinby p_id using betyg_temp.dta , unmatched(master)
 drop _merge

  joinby p_id using sei_pol_par.dta , unmatched(master)
 drop _merge
 

joinby year  using worker_pop_y, unmatched(master)
drop _merge

joinby year age  using worker_pop_ya, unmatched(master)
drop _merge
joinby year mun  using worker_pop_ym, unmatched(master)
drop _merge
joinby year mun woman  using worker_pop_ygm, unmatched(master)
drop _merge
joinby year woman  using worker_pop_yg, unmatched(master)
drop _merge


joinby year age  mun using worker_pop_yam, unmatched(master)
drop _merge
joinby year parti_initial  mun using worker_party_ym, unmatched(master)
drop _merge
joinby valar parti_initial  mun using worker_valu_party_ym, unmatched(master)
drop _merge
joinby year parti_initial using worker_party_y, unmatched(master)
drop _merge
joinby valar parti_initial  using worker_valu_party_y, unmatched(master)
drop _merge
joinby year  using sei_pop_y, unmatched(master)
drop _merge
ren year yeart
gen year =valar
joinby year  using edu_yearmean, unmatched(master)
drop _merge year
ren yeart year

foreach pol in nom vald vald_ri{
bysort p_id:	egen temp=min(valar) if `pol'==1
bysort p_id: egen fy_`pol'=min(temp)
drop temp

foreach var in worker union_worker_prev union_prev{
sort p_id year
gen temp = `var'[_n-1]==1  if fy_`pol'==valar & p_id==p_id[_n-1]
bysort p_id : egen `var'_prev_`pol'	= mode(temp)
drop temp
}	
}
replace party_org=0 if party_org==.
replace party_youth_org=0 if party_youth_org==.
sort p_id year
foreach var in party_org party_youth_org{
	gen `var'_prev=`var'
forvalues n=1/40{ 
replace `var'_prev=1 if `var'[_n-`n']==1  & p_id==p_id[_n-`n']
}
	
}

gen youth_sample=year-age>1956
 gen party_youth_org_lim=  party_youth_org if youth_sample==1


bysort parti_initial party_youth_org_prev:sum worker if vald==1 & youth_sample==1 & valar>=2002

bysort parti_initial worker:sum union_worker_prev union_worker_prev_prev_vald if vald==1  & valar>=2002
gen sei_worker= worker_lim
gen sei_hemp= worker_lim==0
gen sei_miss=sei_imp==.
gen sei_stud=sei_imp==97
gen sei_ret=sei_imp==95
gen stud=sei==97 if sei!=.

egen tag_my= tag(valar llkk)

gen age_b30 = age<30
gen age_30_39 = age>=30 & age<40
gen age_40_49 = age>=40 & age<50
gen age_50_64 = age>=50 & age<64
gen age_ab64 =  age>64
replace stud=0 if stud==.

gen non_uni=ed_level<5 if ed_level!=.
gen non_uni_a25_65=ed_level<5 if ed_level!=. & age>=25 & age<65
gen non_uni_pop= edu1+ edu2 
gen non_uni_a25_65_pop_y= edu1_a25_65 +edu2_a25_65
gen bel25_pct_a25_65 =pct_genage<=25 if   age>=25 & age<65
gen bel25_pct_a25_65_pop_y=25

gen low_inc =pct_genage<=50 if   age>=25 & pct_genage!=.
gen low_inc_pop_y=.50
gen low_inc_pop_ym=.50

joinby ssyk4_seiy using  occ_stdtimeflex, unmatched (master)
drop _merge
joinby ssyk4_seiy using  onet_legtasks , unmatched (master)
drop _merge
gen tasks_abmed= allrelatedtasks >1 if allrelatedtasks !=.
drop if valar==.
duplicates drop p_id valar, force
replace worker_lim=. if year==1998
replace worker_cit=. if year==1998


bysort llkk valar parti_initial: egen ksoparti=max (kso)

***twoway (connected nrinom_vikt nrinom, mcolor(red) msize(zero) msymbol(lgx) mlabel(parti_eng) mlabcolor(red) mlabposition(0) lcolor(red) lpattern(dash))

gen parti_eng=parti_initial
replace  parti_eng ="" if parti_initial=="L"
replace  parti_eng ="L" if parti_initial=="F"
*replace  parti_eng ="Ce" if parti_initial=="C"
replace  parti_eng ="Cd" if parti_initial=="K"
replace  parti_eng ="Sd" if parti_initial=="B"
keep if parti_initial!="" & valar>=1973

foreach var in vald nom vald_lt nom_lt{
gen `var'_prev=0
sort p_id valar
forvalues n=1/10{
	replace `var'_prev=`var'_prev+1 if `var'[_n-`n']==1 & p_id==p_id[_n-`n']
}
replace `var'_prev =5 if `var'_prev>6 & `var'_prev!=.	
}

gen no_local_elec= vald_prev==0 & vald_lt_prev==0


gen listleader=nrinom_hl==1 if vald==1 & nrinom!=.
replace listleader=1 if nrinom==1 & vald==1 & nrinom_hl==. & valar<1990

preserve 
collapse (sum) listleader vald , by( llkk parti_initial valar)
ren listleader num_leader
ren vald num_elec
sort llkk parti_initial valar 
gen  num_leader_lead= num_leader[_n+1] if parti_initial==parti_initial[_n+1] & llkk==llkk[_n+1]
gen  num_elec_lead= num_elec[_n+1] if parti_initial==parti_initial[_n+1] & llkk==llkk[_n+1]
save num_elec_leader, replace
restore

joinby llkk parti_initial valar using num_elec_leader, unmatched(master)
drop _merge
 
gen nom_only=nom==1 & vald!=1 & valar>=1982

gen nom_kp = nom==1 & vald!=1 & chair !=1 & kso!=1 & vald_prev<1 if ksoparti==1
gen vald_kp = vald==1  if ksoparti==1
gen chair_kp = chair ==1 if ksoparti==1
gen kso_kp =  kso==1 if ksoparti==1

gen vald_kp_ad = vald==1  if ksoparti==1 & chair!=1 & kso!=1
gen chair_kp_ad = chair ==1 if ksoparti==1 &vald==1 & kso!=1
gen kso_kp_ad =  kso==1 if ksoparti==1 & (chair==1 | kso==1)

gen position_kp= "nom" if nom==1 & vald!=1 & chair !=1 & kso!=1 &ksoparti==1
replace position_kp="vald" if vald==1 & chair !=1 & kso!=1 & ksoparti==1
replace position_kp="chair" if chair ==1 & kso!=1 & ksoparti==1
replace position_kp="kso" if   kso==1 & ksoparti==1

gen hier_pos=  "nom" if nom==1
replace hier_pos=  "vald" if vald==1
replace hier_pos=  "leader" if listleader==1 & num_leader==1 & num_elec>=5 & num_elec!=. & vald==1
replace hier_pos= "parl" if vald_ri==1

egen k_y_p=group(llkk valar parti_initial)
egen k_y_p_h=group(llkk valar parti_initial position_kp)

egen v_y_p=group(vk valar parti_initial)
sort p_id valar
gen ksoparti_next_remind=ksoparti[_n+1]==1 & llkk==llkk[_n+1] & parti_initial==parti_initial[_n+1] & p_id==p_id[_n+1] &ksoparti==1
bysort llkk valar parti_initial: egen ksoparti_next=max(ksoparti_next_remind) if ksoparti==1


foreach var in vald nom {
	gen `var'_p=0
	sort p_id valar
forvalues n=1/10{
	replace `var'_p=1 if `var'[_n-`n']==1 & p_id==p_id[_n-`n']

}

}

foreach var in  vald_ri listleader nom_kp vald_kp chair_kp kso{
	gen `var'_p=.
	sort p_id valar
forvalues n=1/10{
	replace `var'_p=1 if `var'[_n-`n']==1 & p_id==p_id[_n-`n']
	replace `var'_p=0 if `var'[_n-`n']==0 & p_id==p_id[_n-`n'] & `var'_p==.
}

}




sort p_id valar
gen vald_kp_prom= vald_kp[_n+1]==1 & llkk==llkk[_n+1] & parti_initial==parti_initial[_n+1] & p_id==p_id[_n+1] if ksoparti_next==1 &   nom_kp==1
gen chair_kp_prom= chair_kp[_n+1]==1 & llkk==llkk[_n+1] & parti_initial==parti_initial[_n+1] & p_id==p_id[_n+1] if ksoparti_next==1 &  vald_kp==1
gen kso_prom= kso[_n+1]==1 & llkk==llkk[_n+1] & parti_initial==parti_initial[_n+1] & p_id==p_id[_n+1] if ksoparti_next==1 &   chair_kp==1
gen vald_ri_prom= vald_ri[_n+1]==1 & p_id==p_id[_n+1] if    vald_ri_p!=1 & parti_initial!="" & valar<2018  & vald==1
gen re_elec= vald[_n+1]==1 & p_id==p_id[_n+1] if    valar<2018  & vald==1
gen re_nom= nom[_n+1]==1 & p_id==p_id[_n+1] if    valar<2018  & nom==1
gen vald_ri_altprom= vald_ri[_n+1]==1 & p_id==p_id[_n+1] if    vald_ri_p!=1 & parti_initial!="" & valar<2018  & nom_ri==1
egen mun_prom=rowmax(vald_kp_prom chair_kp_prom kso_prom) if vald_kp_prom!=. | chair_kp_prom!=. | kso_prom!=. 

 gen s_kso = parti_initial=="S" & ksoparti==1
gen ns_kso = parti_initial!="S" & ksoparti==1
gen h_kso = (parti_initial=="M" | parti_initial=="C")  & ksoparti==1
gen smc_kso= ( parti_initial=="S" |parti_initial=="M" | parti_initial=="C")  & ksoparti==1
gen left= nom==1 & (parti_initial=="S" | parti_initial=="V") 
gen notleft= nom==1 & parti_initial!="S" & parti_initial!="V"

gen smc =  (parti_initial=="S" |parti_initial=="M" | parti_initial=="C")
gen all=1
gen vald_prom= vald[_n+1]==1 & llkk==llkk[_n+1] & parti_initial==parti_initial[_n+1] & p_id==p_id[_n+1] if nom==1 & valar<2018 & vald_prev==0 & valar>1980
gen listleader_prom= listleader[_n+1]==1 & llkk==llkk[_n+1] & parti_initial==parti_initial[_n+1] & p_id==p_id[_n+1] if vald==1 & valar<2018 & listleader!=1  & valar>1980 & num_leader==1& num_leader[_n+1]==1

gen worker_lim_un = worker_lim ==1 & union_worker_prev_prev_vald==1 if worker_lim!=.
gen worker_lim_noun = worker_lim ==1 & union_worker_prev_prev_vald==0 if worker_lim!=.

drop union 
joinby p_id valar using studforb, unmatched(master)
foreach var of varlist party- integration{
	replace `var'=0 if `var'==. & valar>=2010
	
}
foreach var of varlist   party_lag- integration_lag{
	replace `var'=0 if `var'==. & valar>=2014
	
}
gen est_sample= num_elec>=5 & num_elec_lead>=5 & num_leader==1 & num_leader_lead==1

preserve
 keep p_id year hier_pos k_y_p parti_initial llkk
 keep if year>=1982 & hier_pos!=""
 save pol_tocomp, replace
 restore
 
 
reghdfe pers_share_parti , abs(k_y_p nrinom_hl) resid
predict pers_res, residual

reghdfe pers_share_parti , abs( nrinom_hl) resid
predict pers_res_alt, residual
foreach var in  pers_res_alt vald_prev pers_res leader betyg_sd cognitive  std_inc_res{
	egen std_`var'=std(`var')
}
 
foreach pos in vald_ri_altprom vald_ri_prom  re_elec re_nom  vald_ri vald_kp_prom chair_kp_prom kso_prom  vald_prom listleader_prom {
 	gen base=`pos' if worker_lim==0
	egen mean=mean(base)
	gen `pos'_rel=`pos'/mean
	drop mean base
 }

stop



gggg
	preserve
	keep if vald==1  & valar>=2002
	collapse (mean) worker worker_pop_ym worker_union_mesh inter_worker_union , by(llkk valar)
		 areg worker worker_pop_ym , abs(valar)
		 	 areg worker  worker_union_mesh  , abs(valar)
		 areg worker worker_pop_ym worker_union_mesh  , abs(valar)
		 		 areg worker worker_pop_ym worker_union_mesh i.valar , abs(llkk)
	 areg worker worker_pop_ym worker_union_mesh inter_worker_union , abs(valar)
	 restore
foreach parti in S V M K C F B G{
	preserve
	keep if vald==1 & parti_initial =="`parti'" & valar>=2002
	collapse (mean) worker worker_pop_ym worker_union_mesh inter_worker_union , by(llkk valar  parti_initial)
	tab  parti_initial
		 areg worker worker_pop_ym , abs(valar)
		 	 areg worker  worker_union_mesh  , abs(valar)
		 areg worker worker_pop_ym worker_union_mesh  , abs(valar)
	 areg worker worker_pop_ym worker_union_mesh inter_worker_union , abs(valar)
	 restore
}

gen sample = 1 if vald==1
replace sample = 2 if vald_ri==1


joinby year sei age using sei_pop_ya, unmatched(master)
drop _merge
joinby year sei mun using sei_pop_ym, unmatched(master)
drop _merge
joinby year sei age mun using sei_pop_yam, unmatched(master)
drop _merge

foreach var in  pct_genage   mar child child_0_18  div single_earner main_earner earn_share {
	gen `var'_yam_adj= `var'-`var'_munmean
}

sort p_id valar
foreach var in  vald nom kso listleader vald_ri {
 gen	 `var'_lag=`var'[_n-1]==1 & p_id==p_id[_n-1] & valar-valar[_n-1]<5

}

foreach var in  vald nom kso listleader vald_ri {
	gen base=valar if `var'==1
	bysort p_id: egen `var'_fy=min(base)
	egen  base2=min(base)
	gen `var'_first= valar ==`var'_fy if `var'==1 & valar!=base2
	drop base base2
}


